/*
 * File     : internal_platform_call_JniNativeMethod.c
 * Created  : 28 Apr 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "common.h"

/*
 * Class:     com_googlecode_dni_internal_call_JniNativeMethod
 * Method:    registerImpl
 * Signature: (Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;J)V
 */
JNIEXPORT void JNICALL Java_com_googlecode_dni_internal_call_JniNativeMethod_registerImpl(
    JNIEnv* env,
    jclass clazz,
    jclass ownerClass,
    jstring name,
    jstring signature,
    jlong fnptr )
{
    JNINativeMethod method;

    method.name = (char*) (*env)->GetStringUTFChars( env, name, NULL );
    if ( method.name == NULL )
    {
    	return;
    }

    method.signature = (char*) (*env)->GetStringUTFChars( env, signature, NULL );
    if ( method.signature == NULL )
    {
    	(*env)->ReleaseStringUTFChars( env, name, method.name );
    	return;
    }

    method.fnPtr = JLONG_TO_POINTER( fnptr );

	(*env)->RegisterNatives( env, ownerClass, &method, 1 );

	(*env)->ReleaseStringUTFChars( env, name, method.name );
	(*env)->ReleaseStringUTFChars( env, signature, method.signature );
}
